package com.linx.common.core.utils;

import cn.hutool.http.HttpRequest;
import cn.hutool.http.HttpResponse;
import com.alibaba.fastjson.JSON;
import com.alibaba.fastjson.JSONObject;
import com.linx.common.core.domain.model.*;
import com.linx.common.core.exception.ServiceException;
import lombok.extern.slf4j.Slf4j;

import java.util.ArrayList;
import java.util.List;


@Slf4j
public class ZYUtil {

    /**
     * 新增区域
     */
    public static ZYResponse addArea(String name) {
        ZYResponse zyResponse = new ZYResponse();

        String url = "https://hzerodevgw.ysyec.com/hitf/v2p/public/rest/invoke/SFpFUk86SU5KQS1FUFM6MTA3OC1hZGRBcmVh";
        JSONObject paramMap = new JSONObject();
        paramMap.put("name", name);

        HttpRequest httpRequest = HttpRequest.post(url).body(paramMap.toJSONString());
        try(HttpResponse execute = httpRequest.execute()) {
            log.info("新增正元区域响应数据：{}", execute.body());

            zyResponse = JSON.parseObject(execute.body(), ZYResponse.class);
        } catch (Exception e) {
            log.info(e.getMessage());
        }

        return zyResponse;
    }

    /**
     * 新增楼栋
     */
    public static ZYResponse addBuilding(ZYBuildingForm buildingForm) {
        ZYResponse zyResponse = new ZYResponse();

        String url = "https://hzerodevgw.ysyec.com/hitf/v2p/public/rest/invoke/SFpFUk86SU5KQS1FUFM6MTA3OC1hZGRCdWlsZGluZw==";
        JSONObject paramMap = new JSONObject();
        paramMap.put("areaID", buildingForm.getAreaID());
        paramMap.put("name", buildingForm.getName());
        if(buildingForm.getFloor() != null && !buildingForm.getFloor().isEmpty()){
            List<JSONObject> floorList = new ArrayList<>();
            buildingForm.getFloor().forEach(floor -> {
                JSONObject subParamMap = new JSONObject();
                subParamMap.put("name", floor.getName());
                subParamMap.put("currentFloor", floor.getCurrentFloor());
                floorList.add(subParamMap);
            });
            paramMap.put("floor", floorList);
        }

        HttpRequest httpRequest = HttpRequest.post(url).body(paramMap.toJSONString());
        try(HttpResponse execute = httpRequest.execute()) {
            log.info("新增正元楼栋响应数据：{}", execute.body());

            zyResponse = JSON.parseObject(execute.body(), ZYResponse.class);
        } catch (Exception e) {
            log.info(e.getMessage());
        }

        return zyResponse;
    }

    /**
     * 新增楼层
     */
    public static ZYResponse addFloor(ZYFloorForm floorForm) {
        ZYResponse zyResponse = new ZYResponse();

        String url = "https://hzerodevgw.ysyec.com/hitf/v2p/public/rest/invoke/SFpFUk86SU5KQS1FUFM6MTA3OC1hZGRGbG9vcg==";

        HttpRequest httpRequest = HttpRequest.post(url).body(JSON.toJSONString(floorForm));
        try(HttpResponse execute = httpRequest.execute()) {
            log.info("新增正元楼层响应数据：{}", execute.body());

            zyResponse = JSON.parseObject(execute.body(), ZYResponse.class);
        } catch (Exception e) {
            log.info(e.getMessage());
        }

        return zyResponse;
    }

    /**
     * 查询楼栋
     */
    public static ZYResponse getBuilding() {
        ZYResponse zyResponse = new ZYResponse();

        String url = "https://hzerodevgw.ysyec.com/hitf/v2p/public/rest/invoke/SFpFUk86SU5KQS1FUFM6MTA3OC1nZXRCdWlsZGluZw==";

        HttpRequest httpRequest = HttpRequest.post(url);
        try(HttpResponse execute = httpRequest.execute()) {
            log.info("查询正元楼栋响应数据：{}", execute.body());

            zyResponse = JSON.parseObject(execute.body(), ZYResponse.class);
        } catch (Exception e) {
            log.info(e.getMessage());
        }

        return zyResponse;
    }

    /**
     * 查询楼层
     */
    public static ZYResponse getFloor() {
        ZYResponse zyResponse = new ZYResponse();

        String url = "https://hzerodevgw.ysyec.com/hitf/v2p/public/rest/invoke/SFpFUk86SU5KQS1FUFM6MTA3OC1nZXRGbG9vcg==";

        HttpRequest httpRequest = HttpRequest.post(url);
        try(HttpResponse execute = httpRequest.execute()) {
            log.info("查询正元楼层响应数据：{}", execute.body());

            zyResponse = JSON.parseObject(execute.body(), ZYResponse.class);
        } catch (Exception e) {
            log.info(e.getMessage());
        }

        return zyResponse;
    }

    /**
     * 新增房间
     */
    public static ZYResponse addRoom(ZYRoomForm form) {
        ZYResponse zyResponse = new ZYResponse();

        String url = "https://hzerodevgw.ysyec.com/hitf/v2p/public/rest/invoke/SFpFUk86SU5KQS1FUFM6MTA4MC1hZGRSb29t";
        log.info("新增正元房间请求数据：{}", JSON.toJSONString(form));

        HttpRequest httpRequest = HttpRequest.post(url).body(JSON.toJSONString(form));
        try(HttpResponse execute = httpRequest.execute()) {
            log.info("新增正元房间响应数据：{}", execute.body());

            zyResponse = JSON.parseObject(execute.body(), ZYResponse.class);
        } catch (Exception e) {
            log.info(e.getMessage());
        }

        return zyResponse;
    }

    /**
     * 修改房间
     */
    public static ZYResponse updateRoom(ZYRoomForm form) {
        ZYResponse zyResponse = new ZYResponse();

        String url = "https://hzerodevgw.ysyec.com/hitf/v2p/public/rest/invoke/SFpFUk86SU5KQS1FUFM6MTA4MC11cGRhdGVSb29t";

        HttpRequest httpRequest = HttpRequest.post(url).body(JSON.toJSONString(form));
        try(HttpResponse execute = httpRequest.execute()) {
            log.info("修改正元房间响应数据：{}", execute.body());

            zyResponse = JSON.parseObject(execute.body(), ZYResponse.class);
        } catch (Exception e) {
            log.info(e.getMessage());
        }

        return zyResponse;
    }

    /**
     * 查询房间
     */
    public static ZYResponse getRoom(ZYRoomForm form) {
        ZYResponse zyResponse = new ZYResponse();

        String url = "https://hzerodevgw.ysyec.com/hitf/v2p/public/rest/invoke/SFpFUk86SU5KQS1FUFM6MTA4MC1nZXRSb29t";
        log.info("查询正元房间请求数据：{}", JSON.toJSONString(form));

        HttpRequest httpRequest = HttpRequest.post(url).body(JSON.toJSONString(form));
        try(HttpResponse execute = httpRequest.execute()) {
            log.info("查询正元房间响应数据：{}", execute.body());

            zyResponse = JSON.parseObject(execute.body(), ZYResponse.class);
        } catch (Exception e) {
            log.info(e.getMessage());
        }

        return zyResponse;
    }

    /**
     * 新增员工
     */
    public static ZYResponse addStaff(ZYStaffForm form) {
        ZYResponse zyResponse = new ZYResponse();

        String url = "https://hzerodevgw.ysyec.com/hitf/v2p/public/rest/invoke/SFpFUk86SU5KQS1FUFM6c3RhZmYtYWRkU3RhZmY=";
        log.info("新增正元员工请求数据：{}", JSON.toJSONString(form));

        HttpRequest httpRequest = HttpRequest.post(url).body(JSON.toJSONString(form));
        try(HttpResponse execute = httpRequest.execute()) {
            log.info("新增正元员工响应数据：{}", execute.body());

            zyResponse = JSON.parseObject(execute.body(), ZYResponse.class);
        } catch (Exception e) {
            log.info(e.getMessage());
        }

        return zyResponse;
    }

    /**
     * 查询员工
     */
    public static ZYResponse getStaff(ZYStaffForm form) {
        ZYResponse zyResponse = new ZYResponse();

        String url = "https://hzerodevgw.ysyec.com/hitf/v2p/public/rest/invoke/SFpFUk86SU5KQS1FUFM6MTA3NS1nZXRTdGFmZg==";
        log.info("查询正元员工请求数据：{}", JSON.toJSONString(form));

        HttpRequest httpRequest = HttpRequest.post(url).body(JSON.toJSONString(form));
        try(HttpResponse execute = httpRequest.execute()) {
            log.info("查询正元员工响应数据：{}", execute.body());

            zyResponse = JSON.parseObject(execute.body(), ZYResponse.class);
        } catch (Exception e) {
            log.info(e.getMessage());
        }

        return zyResponse;
    }


    /**
     * 入住
     */
    public static ZYResponse checkIn(ZYAccessForm form) {
        ZYResponse zyResponse = new ZYResponse();

        String url = "https://hzerodevgw.ysyec.com/hitf/v2p/public/rest/invoke/SFpFUk86SU5KQS1FUFM6MTA4MS1lZGl0QWNjZXNz";
        log.info("正元入住请求数据：{}", JSON.toJSONString(form));

        HttpRequest httpRequest = HttpRequest.post(url).body(JSON.toJSONString(form));
        try(HttpResponse execute = httpRequest.execute()) {
            log.info("正元入住响应数据：{}", execute.body());

            zyResponse = JSON.parseObject(execute.body(), ZYResponse.class);
        } catch (Exception e) {
            log.info(e.getMessage());
        }

        return zyResponse;
    }

    /**
     * 退宿
     */
    public static ZYResponse checkOut(ZYAccessForm form) {
        ZYResponse zyResponse = new ZYResponse();

        String url = "https://hzerodevgw.ysyec.com/hitf/v2p/public/rest/invoke/SFpFUk86SU5KQS1FUFM6MTA4MS1kZWxldGVBY2Nlc3M=";

        HttpRequest httpRequest = HttpRequest.post(url).body(JSON.toJSONString(form));
        try(HttpResponse execute = httpRequest.execute()) {
            log.info("正元退宿响应数据：{}", execute.body());

            zyResponse = JSON.parseObject(execute.body(), ZYResponse.class);
        } catch (Exception e) {
            log.info(e.getMessage());
        }

        return zyResponse;
    }

    /**
     * 获取设备
     */
    public static ZYResponse getDevice() {
        ZYResponse zyResponse = new ZYResponse();

        String url = "https://hzerodevgw.ysyec.com/hitf/v2p/public/rest/invoke/SFpFUk86SU5KQS1FUFM6MTA3Ny1nZXREZXZpY2U=";

        JSONObject paramMap = new JSONObject();
        paramMap.put("queryPage", 1);
        paramMap.put("lineSize", Integer.MAX_VALUE);
        paramMap.put("dataType", "simple-basic");

        log.info("获取正元设备请求数据：{}", paramMap.toJSONString());

        HttpRequest httpRequest = HttpRequest.post(url).body(paramMap.toJSONString());
        try(HttpResponse execute = httpRequest.execute()) {
            log.info("获取正元设备响应数据：{}", execute.body());

            zyResponse = JSON.parseObject(execute.body(), ZYResponse.class);
        } catch (Exception e) {
            log.info(e.getMessage());
        }

        return zyResponse;
    }

    /**
     * 新增人员密码
     */
    public static ZYResponse addPassword(ZYStaffForm form) {
        ZYResponse zyResponse = new ZYResponse();

        String url = "https://hzerodevgw.ysyec.com/hitf/v2p/public/rest/invoke/SFpFUk86SU5KQS1FUFM6MTA3NS1hZGRQYXNzd29yZA==";
        log.info("新增正元人员密码请求数据：{}", JSON.toJSONString(form));

        HttpRequest httpRequest = HttpRequest.post(url).body(JSON.toJSONString(form));
        try(HttpResponse execute = httpRequest.execute()) {
            log.info("新增正元人员密码响应数据：{}", execute.body());

            zyResponse = JSON.parseObject(execute.body(), ZYResponse.class);
        } catch (Exception e) {
            log.info(e.getMessage());
        }

        return zyResponse;
    }

    /**
     * 获取人员密码
     */
    public static ZYResponse getPassword(String staffUniqueID) {
        ZYResponse zyResponse = new ZYResponse();

        String url = "https://hzerodevgw.ysyec.com/hitf/v2p/public/rest/invoke/SFpFUk86SU5KQS1FUFM6MTA3NS1nZXRQYXNzd29yZA==";
        JSONObject paramMap = new JSONObject();
        paramMap.put("staffUniqueID", staffUniqueID);
        log.info("获取正元人员密码请求数据：{}", paramMap.toJSONString());

        HttpRequest httpRequest = HttpRequest.post(url).body(paramMap.toJSONString());
        try(HttpResponse execute = httpRequest.execute()) {
            log.info("获取正元人员密码响应数据：{}", execute.body());

            zyResponse = JSON.parseObject(execute.body(), ZYResponse.class);
        } catch (Exception e) {
            log.info(e.getMessage());
        }

        return zyResponse;
    }


    public static void main(String[] args) {

    }

}
